var loadDiv='<div class="load-container load8"> <div class="loader">Loading...</div> </div>';
var urlhistory={};
$(function() {
	var liLen = $(".menu ul li").size();
	var html = "";
	for (var i = 0; i < liLen; i++) {
		html += '<div class="page">'+loadDiv+'</div>';
	}
	$("#target").html(html);
	$pages = $("#target>.page");
	$(".menu ul li a").click(function(e) {
		console.log(urlhistory);
		e.preventDefault();
		curHref=$(this).attr("href");
		var curPage = curPageIndex;
		var nextPage = $(this).parent().index();
		if($(this).attr("isload")!="false" || isNaN(Number(urlhistory[curHref]))){
			sendRequest("get",curHref, {}, function(data){
				$pages.eq(nextPage).html(data);					
			});
		}
		urlhistory[curHref]=isNaN(Number(urlhistory[curHref]))?1:Number(urlhistory[curHref])+1;
		var AC=getInOutClass(curPage, nextPage);
		doNextPage(curPage, nextPage, AC[0], AC[1]);
	});
	$(".footer>span[target^=tab_]").click(function() {
		var i=$(this).attr("target").substring(4);
		var nextPage=model[i][0];
		$(".menu li>a:eq("+nextPage+")").click(); 
	});
	$(".menu ul li a").first().click();
});

/**
 * ===================================
 */
var curHref="";
var bw = $("body").width();
var $lis = $(".menu ul li");
var $pages;// 等待初始化
var pageSize = $lis.size();
var curPageIndex=pageSize-1;
var animEndEventNames = {
	'WebkitAnimation' : 'webkitAnimationEnd',
	'OAnimation' : 'oAnimationEnd',
	'msAnimation' : 'MSAnimationEnd',
	'animation' : 'animationend'
};
var animEndEventName = animEndEventNames[Modernizr.prefixed('animation')];// 判断事件类型
touch.on('#target', 'swipeleft swiperight', function(ev) {
	if (ev.distance > 120) {// 移动距离
		var curPage = $("#target>.active").index();
		var nextPage;
		console.log(ev);
		if (ev.type == "swiperight") 
			nextPage = curPage - 1;
		if (ev.type == "swipeleft") 
			nextPage = curPage + 1;
		if (nextPage >= pageSize) 
			nextPage = 0;
		if (nextPage < 0) 
			nextPage = pageSize - 1;
		$(".menu li>a:eq("+nextPage+")").click(); 
	}
});
function doNextPage(curPage, nextPage, inClass, outClass) {
	console.log(curPage + ">" + nextPage + ":" + animEndEventName);
	if (curPage != nextPage  ) {
		var $curPage = $pages.eq(curPage), $curLi = $lis.eq(curPage);
		var $nextPage = $pages.eq(nextPage), $nextLi = $lis.eq(nextPage);
		$nextPage.addClass("active");
		$nextLi.addClass("active");
		$curLi.removeClass("active");
		$curPage.addClass(outClass).on(animEndEventName, function() {
			$curPage.off(animEndEventName);
			$curPage.removeClass(outClass);
			$curPage.removeClass("active")
		});
		$nextPage.addClass(inClass).on(animEndEventName, function() {
			$nextPage.off(animEndEventName);
			$nextPage.removeClass(inClass);
			$nextPage.addClass("active");//这里有bug
			window.scrollTo(0,0);
		});
		curPageIndex=nextPage;
		curPageChange(curPageIndex);
	}
}
function curPageChange(curPage){
	var step=0;
	for(var i=0;i<model.length;i++){
		if($.inArray(curPage, model[i])>=0){
			step=i;
			break;
		}
	}
	console.log(step+"==="+curPage);
	for(var i=0;i<model.length;i++){
		var temp=model[i];
		for(var j=0;j<temp.length;j++){
			if(step==i){
				$lis.eq(temp[j]).show();
			}else{
				$lis.eq(temp[j]).hide();
			}	
		}
	}
	$(".footer>span").removeClass("active");
	$(".footer>span[target=tab_"+step+"]").addClass("active").focus();
}
function getInOutClass(curPage,nextPage){
	var allclass =[];
	if (curPage < nextPage) 
		allclass=["pt-page-moveFromRight", "pt-page-moveToLeft"];
	if (curPage > nextPage) 
		allclass=["pt-page-moveFromLeft","pt-page-moveToRight"];
	return allclass;
}

scrollDirect(function(direction) {
if (direction == "down") {
	$(".header .top").hide();
	$(".footer").hide();
}
if (direction == "up") {
	$(".header  .top").show();
	$(".footer").show();
}
});
/**
 * http://www.haorooms.com/post/jquery_scroll_upanddown
 * 
 * 判断滚动条上划下滑事件
 * 
 * @param fn
 */
function scrollDirect(fn) {
    var beforeScrollTop = document.body.scrollTop;
    fn = fn || function () {
    };
    window.addEventListener("scroll", function (event) {
        event = event || window.event;

        var afterScrollTop = document.body.scrollTop;
        delta = afterScrollTop - beforeScrollTop;
        beforeScrollTop = afterScrollTop;

        var scrollTop = $(this).scrollTop();
        var scrollHeight = $(document).height();
        var windowHeight = $(this).height();
        if (scrollTop + windowHeight > scrollHeight - 10) {  //滚动到底部执行事件
//            fn('up');
            return;
        }
        if (afterScrollTop < 10 || afterScrollTop > $(document.body).height - 10) {
            fn('up');
        } else {
            if (Math.abs(delta) < 10) {
                return false;
            }
            fn(delta > 0 ? "down" : "up");
        }
    }, false);
}

//==============================================
window.onload=function(){  
    changeBody();  
}  
//当浏览器窗口大小改变时，设置显示内容的高度  
window.onresize=function(){  
	changeBody();  
}  
function changeBody(){               
   var w = window.screen.width;
   $(".header,.footer").css("width",w+"px");
}